package com.wjv.example.springboot.base.event;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * <p>Title: 事件监听器 </p>
 *
 * @author wjv
 * @date 2021/6/16 10:23
 */
@Component
public class SentMqEventListener {
    @EventListener()
    public void sentMq1(SentMqEvent sentMqEvent) {
        System.out.println("无事务");
        System.out.println(sentMqEvent);
        System.out.println(sentMqEvent.getUserInfoEntity());
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void sentMq2(SentMqEvent sentMqEvent) {
        System.out.println("事务提交了");
        System.out.println(sentMqEvent);
        System.out.println(sentMqEvent.getUserInfoEntity());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行完成");
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
    public void sentMq3(SentMqEvent sentMqEvent) {
        System.out.println("事务回滚了");
        System.out.println(sentMqEvent);
        System.out.println(sentMqEvent.getUserInfoEntity());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行完成");
    }
}
